package com.yuanmei.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;

/**
 * Created by loro on 2017/2/8.
 */
public class TaCombinedChart extends CombinedChart {
    private LeftMarkerView mLeftMarkerView;
    private HorizontalMarkerView mHorizontalMarkerView;
    private BottomMarkerView mBottomMarkerView;
    private TaChartData mTaChartData;

    public TaCombinedChart(Context context) {
        super(context);
    }

    public TaCombinedChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TaCombinedChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setLeftMarkerView(LeftMarkerView markerLeft) {
        this.mLeftMarkerView = markerLeft;
    }

    public void setBottomMarkerView(BottomMarkerView markerBottom) {
        this.mBottomMarkerView = markerBottom;
    }

    public void setHorizontalMarkerView(HorizontalMarkerView markerH) {
        this.mHorizontalMarkerView = markerH;
    }

    public void setTaChartData(TaChartData data) {
        this.mTaChartData = data;
    }

    @Override
    protected void drawMarkers(Canvas canvas) {
        if (!mDrawMarkers || !valuesToHighlight())
            return;
//        for (int i = 0; i < mIndicesToHighlight.length; i++) {
//            Highlight highlight = mIndicesToHighlight[i];
        for(Highlight highlight : mIndicesToHighlight) {
            float highlightX = highlight.getX();
            float deltaX = mXAxis != null
                    ? mXAxis.mAxisRange : 0.f;
            if (highlightX <= deltaX && highlightX <= deltaX * mAnimator.getPhaseX()) {
                Entry e = mData.getEntryForHighlight(highlight);
                // make sure entry not null
                if (e == null || e.getX() != highlight.getX())
                    continue;
                float[] pos = getMarkerPosition(highlight);
                // check bounds
                if (!mViewPortHandler.isInBounds(pos[0], pos[1]))
                    continue;

                if (null != mHorizontalMarkerView) {
                    mHorizontalMarkerView.refreshContent(e, highlight);
                    int width = (int) mViewPortHandler.contentWidth();
                    mHorizontalMarkerView.setTvWidth(width);
                    mHorizontalMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    mHorizontalMarkerView.layout(0, 0, width,
                            mHorizontalMarkerView.getMeasuredHeight());
                    mHorizontalMarkerView.draw(canvas, mViewPortHandler.contentLeft(),
                            highlight.getYPx() - mHorizontalMarkerView.getHeight() / 2);
                }

                if (null != mLeftMarkerView) {
                    //修改标记值
                    float yValForHighlight = highlight.getY();
                    mLeftMarkerView.setData(yValForHighlight);

                    mLeftMarkerView.refreshContent(e, highlight);

                    mLeftMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    mLeftMarkerView.layout(0, 0, mLeftMarkerView.getMeasuredWidth(),
                            mLeftMarkerView.getMeasuredHeight());

                    mLeftMarkerView.draw(canvas, mViewPortHandler.contentLeft(),
                            highlight.getYPx() - mLeftMarkerView.getHeight() / 2);

                }

                if (null != mBottomMarkerView && mTaChartData != null) {
                    String date = mTaChartData.getXAxisLabel((int)highlightX);

                    mBottomMarkerView.setData(date);
                    mBottomMarkerView.refreshContent(e, highlight);

                    mBottomMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    mBottomMarkerView.layout(0, 0, mBottomMarkerView.getMeasuredWidth(),
                            mBottomMarkerView.getMeasuredHeight());

                    mBottomMarkerView.draw(canvas, pos[0] - mBottomMarkerView.getWidth(),
                            mViewPortHandler.contentBottom());
                }
            }
        }
    }
}
